using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Rhino.Mocks;
using WWB.SitePointPrivateMessages.Core.Infrastructure.Logging;
using WWB.SitePointPrivateMessages.Core.Model.DataImport;
using WWB.SitePointPrivateMessages.Core.Model.Messages;

namespace WWB.SitePointPrivateMessages.Tests.DataImportTests
{
    [TestFixture]
    public class ConvertToMessageTests
    {
        private ExportedMessage testMessage;

        [SetUp]
        public void Setup_Message()
        {
            testMessage = new ExportedMessage()
                              {
                                  DateStamp = "2010-06-17 23:51",
                                  Title = "signature links",
                                  FromUser = "SampleMember",
                                  FromUserId = 83449,
                                  ToUser = "MySigLink",
                                  MessageBody =
                                      "Hi MySigLink,\r\rPlease wait until you get your signature for your links.\rPlease read [URL=\"http://www.sitepoint.com/forums/showthread.php?t=669114\"]Signatures at SitePoint[/URL]  and [URL=\"http://www.sitepoint.com/forums/faq.php?faq=youraccount#faq_signatures\"]Signatures FAQ[/URL]\r\rthanks, SampleMember"
                              };
        }

        [Test]
        public void Exported_message_is_properly_mapped()
        {
            var userlist = new List<ForumUser>();
            var msg = testMessage.ConvertToMessage(userlist, MockRepository.GenerateStub<ILogger>());
            msg.AssertPrivateMessageMatches(testMessage);
        }

        [Test]
        public void Users_are_added()
        {
            var userlist = new List<ForumUser>();
            var msg = testMessage.ConvertToMessage(userlist, MockRepository.GenerateStub<ILogger>());
            var usr = userlist.FirstOrDefault(x => x.Name == "MySigLink");
            Assert.That(usr, Is.Not.Null, "Could not find user 'MySigLink'");
            usr = userlist.FirstOrDefault(x => x.Id == 83449);
            Assert.That(usr, Is.Not.Null, "Could not find user 83449");
            Assert.That(usr.Name, Is.EqualTo("SampleMember"));
            Assert.That(userlist.Count, Is.EqualTo(2));
        }

        [Test]
        public void User_mapped_when_found()
        {
            var userList = new List<ForumUser>() {new ForumUser() {Id = 42, Name = "MySigLink"}};
            var msg = testMessage.ConvertToMessage(userList, MockRepository.GenerateStub<ILogger>());
            var usr = msg.ToUsers.FirstOrDefault();
            Assert.That(usr.Id, Is.EqualTo(42));
        }
    }
}